package utils

import java.security.Security
import java.util.Properties
import javax.mail._
import javax.mail.internet._

object SendEmail {


  def main(args: Array[String]): Unit = {
    val sender = Sender("","smtp.126.com","modmslzu2020@126.com","DLUIIDMTBXEDIDHG")
    val info = Info("GRNA task：CQXqpvWhVxtPTyz2jKyUP is running completed","Click https://modms.lzu.edu.cn/alfalfa/tools/grna/CQXqpvWhVxtPTyz2jKyUP to view the result page")
    sendEmailBySsl(sender,info,"496200437@qq.com")
  }
  case class Sender(nick: String, host: String, email: String, password: String)

  case class Info(subject: String, content: String)

  def sendEmailBySsl(sender: Sender, info: Info, inbox: String) = {
    val props = new Properties()
    props.put("mail.smtp.auth", "true")
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider())
    val SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY)
    props.setProperty("mail.smtp.socketFactory.fallback", "false")
    props.setProperty("mail.smtp.port", "465")
    props.setProperty("mail.smtp.socketFactory.port", "465")
    val mailSession = Session.getDefaultInstance(props)
    val transport = mailSession.getTransport("smtp")
    val message = new MimeMessage(mailSession)
    val nick = MimeUtility.encodeText(sender.nick)
    message.setSubject(info.subject)
    message.setFrom(new InternetAddress(s"${nick}<${sender.email}>"))
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(inbox))
    message.setContent(info.content, "text/html;charset=utf-8")
    println(info)
    println(inbox)
    try{
      transport.connect(sender.host, sender.email, sender.password)
      transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO))
      transport.close()

    }catch {
      case e:Exception=>
        println(e)
        println(e.getMessage)
    }

  }

}
